package com.shop.controller;

import com.alibaba.druid.util.StringUtils;
import com.shop.pojo.*;
import com.shop.service.DtsBrandService;
import com.shop.service.DtsCategoryService;
import com.shop.service.DtsGoodsService;
import com.shop.util.ResponseUtil;
import com.shop.vo.CatVo;
import com.shop.vo.GoodsAllinone;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName AdminGoodsController
 * @Author xiao宋
 * @Date 19:54  2022/10/17
 * @Version 1.0
 * @Description TODO
 */
@RestController
@Slf4j
@RequestMapping("/admin/goods")
public class AdminGoodsController {
    @Autowired
    DtsGoodsService goodsService;
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCategoryService categoryService;
    @GetMapping("/detail")
    public Object detail(@RequestParam("id")Integer goodsId){
        Map<String, Object> data = goodsService.detail(goodsId);
        return ResponseUtil.ok(data);
    }
@PostMapping("/update")
public Object update(@RequestBody GoodsAllinone goodsAllinone){
    Object result = checkGoodAllinone(goodsAllinone);
    if (result !=null){
        return result;
    }
    try {
        goodsService.update(goodsAllinone);
    }catch (Exception e){
        e.printStackTrace();
        return ResponseUtil.fail(-1,e.getMessage());
    }
    return ResponseUtil.ok();
}
    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        goodsService.delete(goods);
        return ResponseUtil.ok();
    }
    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone) {
        log.debug("添加商品方法开始执行");
        Object result = checkGoodAllinone(goodsAllinone);
        if (result != null) {
            return result;
        }
        try {
            goodsService.insert(goodsAllinone);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(-1, e.getMessage());
        }
        return ResponseUtil.ok();
    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand() {
        List<CatVo> categoryList = categoryService.findCategoryList();
        List<Map<String, Object>> brandList = brandService.findBrandList();

        Map<String, Object> data = new HashMap<>();
        data.put("categoryList", categoryList);
        data.put("brandList", brandList);
        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object list(
            @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
            @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit,
            @RequestParam(value = "sort", required = false, defaultValue = "add_time") String sort,
            @RequestParam(value = "order", required = false, defaultValue = "desc") String order,
            String goodsSn,
            String name
    ) {
//        获取当前登录的用户
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
//    是品牌制造商就查对应的，
        if (subject.hasRole("品牌制造商")) {
            brandIds = brandService.findBrandIdsByAdminId(admin.getId());
        }
        Map<String, Object> data = goodsService.findByPage(page, limit, goodsSn, name, order, sort, brandIds);
        return ResponseUtil.ok(data);
    }

    private Object checkGoodAllinone(GoodsAllinone goodsAllinone) {
        DtsGoods goods = goodsAllinone.getGoods();
        if (goods == null || StringUtils.isEmpty(goods.getName()) || StringUtils.isEmpty(goods.getGoodsSn() )) {
            return ResponseUtil.fail(-1, "商品名称和商品编号不能为空");
        }
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if (attributes != null && attributes.length > 0) {
            for (DtsGoodsAttribute attribute : attributes) {
                if (StringUtils.isEmpty(attribute.getAttribute())) {
                    return ResponseUtil.fail(-1, "商品属性名不能为空");
                }
            }
        }
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if (specifications != null && specifications.length > 0) {
            for (DtsGoodsSpecification specification : specifications) {
                if (StringUtils.isEmpty(specification.getSpecification())) {
                    return ResponseUtil.fail(-1, "商品规格名不能为空");
                }
            }
        }
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if (products == null || products.length == 0) {
            return ResponseUtil.fail(-1, "必须设置库存参数");
        }
//        商品品牌
        Integer brandId = goods.getBrandId();
        if (brandId == null) {
            DtsBrand brand = brandService.findById(brandId);
            if (brand == null) {
                return ResponseUtil.fail(-1, "商品品牌不存在");
            }
        }
//        判断分类
        Integer categoryId = goods.getCategoryId();
        if (categoryId != null) {
            DtsCategory category = categoryService.findById(categoryId);
            if (category == null) {
                return ResponseUtil.fail(-1, "没有该商品分类");
            }
        }
        return null;
    }
}
